<?php


namespace app\api\controller\v1;


use app\api\model\WarehouseModel;
use app\common\controller\UserBaseController;

class WarehouseController extends UserBaseController
{

    function infoAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new WarehouseModel())->query_info($this->city_id, $this->argv['id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function listAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'state' => 'optint',
            'start_time' => 'optstring',
            'end_time' => 'optstring',
            'sk' => 'optstring',
            'pno' => 'int',
            'psize' => 'int',
            'city_id' => 'optstring',
        );

        $this->check_params();
        $this->check_token();

        $list = (new WarehouseModel())->query_list($this->city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;
        if (empty($list)) {
            $this->resp['total'] = 0;
        } else {
            $this->resp['total'] = hisql::query_count();
        }

        return $this->ret_resp();
    }

    function addAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'coordinate' => 'array',
            'address' => 'string',
            'state' => 'int',
            'purpose_type' => 'string',
            'entity_type' => 'string',
            'purpose' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $infox = (new WarehouseModel())->add($this->argv);
        $this->resp['info'] = $infox;

        return $this->ret_resp();
    }

    function updateAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
            'name' => 'optstring',
            'remark' => 'optstring',
            'address' => 'optstring',
            'state' => 'optint',
            'purpose_type' => 'optstring',
            'entity_type' => 'optstring',
            'purpose' => 'optstring',
            'coordinate' => 'optarray',
        );

        $this->check_params();
        $this->check_token();

        $m = new WarehouseModel();

        $info = (new WarehouseModel())->query_info($this->city_id, $this->argv['id']);
        if (empty($info)) {
            $this->throw_myexception(X_ERROR, "查询仓库信息失败！");
        }

        $data = [];
        foreach ($this->argv as $k => $v) {
            if (isset($info[$k]) && $this->argv[$k] == $v) {
                continue;
            }
            $data[$k] = $v;
        }

        (new WarehouseModel())->update($this->city_id, $this->argv['id'], $data);

        return $this->ret_resp();
    }

    function delAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new WarehouseModel())->delete_warehouse($this->city_id, $this->argv['id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

}
